package com.hrms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrms.entity.Employee;
import com.hrms.mapper.EmployeeMapper;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    public List<Map<String, Object>> selectAll() {
        // 返回表employee的所有记录
        // 排除占位记录即employee_id为000的记录
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.ne("employee_id",0);

        return listMaps(wrapper);
    }

    public boolean passwordisRight(Employee employee) {
        // 根据employee_id和employee_password查询工号密码是否匹配
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("employee_id", employee.getEmployee_id())
                .eq("employee_password", employee.getEmployee_password());
        int count = (int) count(wrapper);

        if(count == 0) return false;
        else return true;
    }

    public boolean idExist(Employee employee) {
        // 查询账号是否存在
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("employee_id", employee.getEmployee_id());
        int count = (int) count(wrapper);

        if(count == 0) return false;
        else return true;
    }

    public boolean checkStatue(Employee employee) {
        // 根据employee_id和employee_status查询工号状态是否匹配
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("employee_id", employee.getEmployee_id())
                .eq("employee_status", employee.getEmployee_status());
        int count = (int) count(wrapper);

        if(count == 0) return false;
        else return true;
    }

    public boolean updateStatue(Employee employee) {
        // 根据employee_id修改employee_status
        // true表示修改成功
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("employee_id", employee.getEmployee_id());

        return update(employee, wrapper);
    }

    public boolean add(Employee employee) {
        // 在表employee中添加一条记录
        // true表示添加成功
        return save(employee);
    }

    public boolean updatebyAdmin(Employee employee) {
        // 根据employee_id修改employee_password和employee_name
        // true表示修改成功
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("employee_id", employee.getEmployee_id());

        return update(employee, wrapper);
    }

    public String selectEmployeeName(Employee employee) {
        // 根据employee_id返回employee_name
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("employee_id", employee.getEmployee_id())
                .select("employee_name");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("employee_name").toString();
    }
}
